package vitaliy.bondar.diamondcave;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.util.GLState;
import org.andengine.util.color.Color;

import vitaliy.bondar.diamondcave.SceneManager.SceneType;

public class MainMenuScene extends BaseScene implements IOnMenuItemClickListener{

    private MenuScene menuChildScene;
    private final int MENU_PLAY = 0;
    private final int MENU_OPTIONS = 1;
    
    @Override
    public void createScene() {
        createBackground();
        createMenuChildScene();
    }

    @Override
    public void onBackKeyPressed() {
        System.exit(0);
    }

    @Override
    public SceneType getSceneType()
    {
        return SceneType.SCENE_MENU;
    }

    @Override
    public void disposeScene()
    {
        activity.runOnUpdateThread(new Runnable()
        {
            @Override
            public void run()
            {
                setChildrenIgnoreUpdate(true);
                setIgnoreUpdate(true);
                detachChildren();
                ResourcesManager.getInstance().unloadMenuResources();
            }
        });
    }
    
    private void createBackground()
    {
        attachChild( new Sprite( 0, 0, resourcesManager.menu_background_region, vbom )
        {
            @Override
            protected void preDraw(GLState pGLState, Camera pCamera) 
            {
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
        });
    }
    
    private void createMenuChildScene()
    {
        menuChildScene = new MenuScene(camera);
        
        final IMenuItem playMenuItem = new ScaleMenuItemDecorator(
                new SpriteMenuItem(MENU_PLAY, resourcesManager.menu_button_region, vbom), 1.2f, 1);
        final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(
                new SpriteMenuItem(MENU_OPTIONS, resourcesManager.menu_button_region, vbom), 1.2f, 1);
        
        menuChildScene.addMenuItem(playMenuItem);
        menuChildScene.addMenuItem(optionsMenuItem);
        
        menuChildScene.buildAnimations();
        menuChildScene.setBackgroundEnabled(false);
        
        playMenuItem.setPosition(playMenuItem.getX(), playMenuItem.getY() + 30);
        optionsMenuItem.setPosition(optionsMenuItem.getX(), optionsMenuItem.getY() + 60);
        
        final Text playLabel = new Text(0, 0, 
                resourcesManager.menu_font, activity.getString(R.string.play), vbom);
        
        final Text optionsLabel = new Text(0, 0, 
                resourcesManager.menu_font, activity.getString(R.string.options), vbom);

        playLabel.setColor(Color.BLACK);
        optionsLabel.setColor(Color.BLACK);
        
        playLabel.setPosition(playMenuItem.getWidth() / 2 - playLabel.getWidth() / 2, 
                playMenuItem.getHeight() / 2 - playLabel.getHeight() / 2 + 10);
        optionsLabel.setPosition(optionsMenuItem.getWidth() / 2 - optionsLabel.getWidth() / 2, 
                optionsMenuItem.getHeight() / 2 - optionsLabel.getHeight() / 2 + 10);
        
        playMenuItem.attachChild(playLabel);
        optionsMenuItem.attachChild(optionsLabel);
        
        menuChildScene.setOnMenuItemClickListener(this);
        
        setChildScene(menuChildScene);
    }
    
    public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, 
            float pMenuItemLocalX, float pMenuItemLocalY)
    {
            switch(pMenuItem.getID())
            {
            case MENU_PLAY:
                SceneManager.getInstance().loadSelectorScene( engine );
                return true;
            case MENU_OPTIONS:
            	SceneManager.getInstance().loadOptionsScene( engine );
                return true;
            default:
                return false;
        }
    }
}
